/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddTaxi.java
 *
 * Created on Mar 8, 2011, 11:14:11 AM
 */

package Taxi;

import Taxi.model.Taxi;
import Taxi.db.TaxiDAO;
import java.sql.SQLException;
/**
 *
 * @author BIGBOSS
 */
public class AddTaxiUI extends javax.swing.JFrame {

    public boolean saveMode = true;

    /** Creates new form AddTaxi */
    public AddTaxiUI() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jFormattedTextField1 = new javax.swing.JFormattedTextField();
        jFormattedTextField2 = new javax.swing.JFormattedTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jTextField7 = new javax.swing.JTextField();
        jTextField8 = new javax.swing.JTextField();
        jTextField9 = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jTextField10 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        jLabel1.setText("Taxi number");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(60, 10, 100, 14);
        getContentPane().add(jTextField1);
        jTextField1.setBounds(230, 10, 116, 20);

        jLabel2.setText("Plate Number");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(60, 40, 90, 14);

        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });
        getContentPane().add(jTextField2);
        jTextField2.setBounds(230, 40, 116, 20);

        jLabel3.setText("Start Date");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(60, 70, 90, 14);

        jLabel4.setText("Start Odometer");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(60, 130, 110, 14);
        getContentPane().add(jTextField4);
        jTextField4.setBounds(230, 130, 110, 20);

        jLabel5.setText("Original Color");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(60, 190, 100, 14);
        getContentPane().add(jTextField5);
        jTextField5.setBounds(230, 190, 110, 20);

        jButton1.setText("Save");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(410, 410, 65, 23);

        jButton2.setText("Cancel");
        jButton2.setActionCommand("Cancel      ");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(410, 440, 65, 23);

        jLabel6.setText("Brand New/Second hand");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(60, 220, 150, 14);
        getContentPane().add(jTextField6);
        jTextField6.setBounds(260, 220, 28, 20);

        jLabel7.setText("(B/S)");
        getContentPane().add(jLabel7);
        jLabel7.setBounds(220, 220, 40, 14);

        jLabel8.setText("End Date");
        getContentPane().add(jLabel8);
        jLabel8.setBounds(60, 100, 80, 14);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(171, 409, 116, 60);
        getContentPane().add(jFormattedTextField1);
        jFormattedTextField1.setBounds(230, 70, 116, 20);
        getContentPane().add(jFormattedTextField2);
        jFormattedTextField2.setBounds(230, 100, 116, 20);

        jLabel9.setText("yyyy-mm-dd");
        getContentPane().add(jLabel9);
        jLabel9.setBounds(360, 70, 110, 14);

        jLabel10.setText("yyyy-mm-dd");
        getContentPane().add(jLabel10);
        jLabel10.setBounds(360, 100, 100, 14);

        jLabel11.setText("Engine Number");
        getContentPane().add(jLabel11);
        jLabel11.setBounds(60, 250, 110, 14);

        jLabel12.setText("Chasisnumber");
        getContentPane().add(jLabel12);
        jLabel12.setBounds(60, 290, 120, 14);

        jLabel13.setText("Green Plate");
        getContentPane().add(jLabel13);
        jLabel13.setBounds(60, 330, 100, 14);

        jLabel14.setText("Yellow Plate");
        getContentPane().add(jLabel14);
        jLabel14.setBounds(60, 370, 120, 14);
        getContentPane().add(jTextField3);
        jTextField3.setBounds(230, 250, 106, 20);
        getContentPane().add(jTextField7);
        jTextField7.setBounds(230, 290, 106, 20);
        getContentPane().add(jTextField8);
        jTextField8.setBounds(230, 330, 106, 20);
        getContentPane().add(jTextField9);
        jTextField9.setBounds(230, 370, 106, 20);

        jLabel15.setText("End Odometer");
        getContentPane().add(jLabel15);
        jLabel15.setBounds(60, 160, 130, 14);
        getContentPane().add(jTextField10);
        jTextField10.setBounds(230, 160, 110, 20);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-552)/2, (screenSize.height-521)/2, 552, 521);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_jButton2ActionPerformed

     public void setData(Taxi taxi){

        jTextField1.setText(taxi.getUnitNumber()+"");
        jTextField2.setText(taxi.getPlateNumber());
        jFormattedTextField1.setText(taxi.getStartDate());
        jFormattedTextField2.setText(taxi.getEndDate());
        jTextField4.setText(taxi.getStartOdometer()+"");
        jTextField10.setText(taxi.getEndOdometer()+"");
        jTextField5.setText(taxi.getOrigColor());
        jTextField6.setText(taxi.getNewOld());
        jTextField3.setText(taxi.getEngineNumber());
        jTextField7.setText(taxi.getChasisNumber());
        jTextField8.setText(taxi.getGreenPlate());
        jTextField9.setText(taxi.getYellowPlate());
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        Taxi taxi = new Taxi();
        int x = Integer.parseInt(jTextField1.getText());
        taxi.setUnitNumber(x);
        taxi.setPlateNumber(jTextField2.getText());
        taxi.setStartDate(jFormattedTextField1.getText());
        taxi.setEndDate(jFormattedTextField2.getText());
        int y = Integer.parseInt( jTextField4.getText() );
        taxi.setStartOdometer(y);
        int z = Integer.parseInt( jTextField10.getText() );
        taxi.setEndOdometer(z);
        taxi.setOrigColor(jTextField5.getText());
        taxi.setNewOld(jTextField6.getText());
        taxi.setEngineNumber(jTextField3.getText());
        taxi.setChasisNumber(jTextField7.getText());
        taxi.setGreenPlate(jTextField8.getText());
        taxi.setYellowPlate(jTextField9.getText());
        


        TaxiDAO taxiDAO = new TaxiDAO();
        try{
            int i;
              if (saveMode)
                i = taxiDAO.save(taxi);
            else
                i = taxiDAO.update(taxi);
            jTextArea1.append("Inserted " + i + " rows successfully");
        } catch (SQLException insertException) {
            displaySQLErrors(insertException);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField2ActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_jTextField2ActionPerformed

 

   private void displaySQLErrors(SQLException e) {
  jTextArea1.append("SQLException: " + e.getMessage() + "\n");
  jTextArea1.append("SQLState:     " + e.getSQLState() + "\n");
  jTextArea1.append("VendorError:  " + e.getErrorCode() + "\n");
  }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JFormattedTextField jFormattedTextField1;
    private javax.swing.JFormattedTextField jFormattedTextField2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField10;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField jTextField8;
    private javax.swing.JTextField jTextField9;
    // End of variables declaration//GEN-END:variables


}
